package com.mx.selenium;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import com.jayway.restassured.RestAssured;
import com.jayway.restassured.path.json.JsonPath;
import com.jayway.restassured.response.Response;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.jayway.restassured.RestAssured.get;
import static org.junit.Assert.assertEquals;

/**
 * Created by miaoxie on 2017/3/24 16:03.
 */
public class SFbug {
    @Before
    public void setUp() {
        RestAssured.baseURI = "";
        RestAssured.port = 80;

    }
    List<SFBugEntity> level2 = new ArrayList<SFBugEntity>();
    @Test
    public void testUserLogin_JsonPath() {

        level2 = getAddressList("A000086000",-1,"sc");
        getChildList(level2);
        for (SFBugEntity entity : level2){

                getChildList(entity.getSfBugEntityList());

        }
        List<SFDeptEntity> writeList = new ArrayList<SFDeptEntity>();
        ergodic();
        for(SFBugEntity province :level2){
            for(SFBugEntity city : province.getSfBugEntityList()){
                if(city.getLevel() == 3){
                    for(SFBugEntity town : city.getSfBugEntityList()){
                        writeList.addAll(town.getDeptEntitieList());

                    }
                }else if(city.getLevel()==4){
                    writeList.addAll(city.getDeptEntitieList());
                }
            }
        }
        ExportExcel.exportPerson("",writeList);
        System.out.println("");

    }
    public void ergodic(){

        for(SFBugEntity province : level2){
            for(SFBugEntity city : province.getSfBugEntityList()){
                if(city.getLevel() == 3){
                    for(SFBugEntity town : city.getSfBugEntityList()){
                        String addName = province.getName()+"-"+city.getName()+"-"+town.getName();
                        String coordinate = getIndex(addName);
                        if(town.getDeptEntitieList() == null){
                            town.setDeptEntitieList(new ArrayList<SFDeptEntity>());
                        }
                        town.getDeptEntitieList().addAll(getDeptBycoordinate(coordinate));

                    }
                }else {
                    String addName = province.getName()+"-"+city.getName();
                    String coordinate = getIndex(addName);
                    if(city.getDeptEntitieList() == null){
                        city.setDeptEntitieList(new ArrayList<SFDeptEntity>());
                    }
                    city.getDeptEntitieList().addAll(getDeptBycoordinate(coordinate));
                }
            }
        }
    }
    public boolean isLevelFour(List<Object> list){
        Map map = (Map) list.get(0);
        return Integer.parseInt(String.valueOf(map.get("level")))==4;
    }
    public void getChildList(List<SFBugEntity> list){
        List<SFBugEntity> clone = new ArrayList<SFBugEntity>(list);
        int i = 0;
       for(SFBugEntity entity : clone){
           if(entity.getLevel() == 4){
               continue;
           }
           List<SFBugEntity> listTemp = getAddressList(entity.getCode(),entity.getLevel(),entity.getLang());
           int j = 0;
           List<SFBugEntity> clone2 = new ArrayList<SFBugEntity>(listTemp);
           if(list.get(i).getSfBugEntityList() == null){
               list.get(i).setSfBugEntityList(new ArrayList<SFBugEntity>());
           }
           list.get(i).getSfBugEntityList().addAll(listTemp);
           i++;
       }
    }
    public List<SFBugEntity> getAddressList(String parentId,Integer level,String lang){
        String url = "http://www.sf-express.com/sf-service-owf-web/service/region/"+parentId+"/subRegions?level="+level+"&lang="+lang+"&region=cn&translate=";
        Response response = get(url);
        assertEquals(200, response.getStatusCode());
        String json = response.asString();
        JSONArray jsonObject = JSONArray.fromObject(json);
        Map<String,List<SFBugEntity>> map = new HashMap<String, List<SFBugEntity>>();
        List<SFBugEntity> levels= JSONArray.toList(jsonObject,SFBugEntity.class);

        return levels;
    }

    public String getIndex(String address){
        String url = "http://restapi.amap.com/v3/geocode/geo?key=389880a06e3f893ea46036f030c94700&s=rsv3&city=35&address="+address;
        Response response = get(url);

        assertEquals(200, response.getStatusCode());
        String json = response.asString();
        JSONObject jsonObject = JSONObject.fromObject(json);
        List zblist= (List) jsonObject.get("geocodes");
        String zb = (String) ((Map)zblist.get(0)).get("location");
        return zb;
    }
    public List<SFDeptEntity> getDeptBycoordinate(String add){
        String []coordinate = add.split(",");
        String longitude = coordinate[0];
        String latitude =coordinate[1];
        String url = "http://www.sf-express.com/sf-service-owf-web/service/store?lang=sc&region=cn&translate=&longitude="+longitude+"&latitude="+latitude+"&range=3000.0";
        Response response = get(url);
        assertEquals(200, response.getStatusCode());
        String json = response.asString();
        JSONArray jsonObject = JSONArray.fromObject(json);
        List<SFDeptEntity> deptEntities = JSONArray.toList(jsonObject,SFDeptEntity.class);
        return deptEntities;
    }
    @Test
    public void test(){
        List<SFDeptEntity> sf = new ArrayList<SFDeptEntity>();
        SFDeptEntity sfDeptEntity = new SFDeptEntity();
        sfDeptEntity.setAddress("aaaaaaaaaa");
        sfDeptEntity.setCode("ljljo");
        sfDeptEntity.setLat((float) 0.235646);

        sf.add(sfDeptEntity);

        for (SFDeptEntity sfDeptEntity1 : sf){
            sfDeptEntity1.setAddress("ssssss");
        }
        ExportExcel.exportPerson("aa",sf);
    }
}
